package com.wenx.throwCamel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.FailedToCreateRouteException;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

import javax.wsdl.WSDLException;
import java.io.IOException;

public class JettyThrow1 {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    public static RouteDefinition initRoute(){
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition
                .from("jetty:http://localhost:9080/camel")
                .doTry()
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        exchange.getOut().setHeader("SOAPAction","none");
                        System.out.println("访问成功!");
                        String content="<?xml version=\"1.0\" encoding=\"utf-8\"?>  <soap:Envelope"
                                + " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                                + "<soap:Body> " +
                                "        <sayHello xmlns=\"http://test.axis1.com/\"> " +
                                "            <id>123</id>  " +
                                "            <name>张三</name>  " +
                                "        </sayHello> " +
                                "    </soap:Body> "
                                + "</soap:Envelope>";
                        exchange.getOut().setBody(content);
                    }
                })
                .to("cxf:"
                                + "http://localhost:8086/services/axis1webservice" //service address
                                + "?"
                                + "wsdlURL=http://localhost:8086/services/axis1webservice?wsdl"    //wsdl url
                                + "&"
                                + "dataFormat=MESSAGE"
                                +         //dataformat type
                                "&" +
                                "endpointName=axis1webservice"
                )
                .doCatch(FailedToCreateRouteException.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("FailedToCreateRouteException!");
                        exchange.getOut().setBody("FailedToCreateRouteException");
                    }
                })
                .doCatch(WSDLException.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("出现WSDL异常!");
                        exchange.getOut().setBody("出现WSDL异常");
                    }
                })
                .doCatch(IOException.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("出现IO异常!");
                        exchange.getOut().setBody("出现IO异常");
                    }
                })
                .doCatch(Exception.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("出现异常!");
                        exchange.getOut().setBody("出现异常");
                    }
                })
                .endDoTry();
        return routeDefinition;
    }
}
